package service.impl;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import model.Treceipt;
import model.Tsalereturn;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.ReceiptDaoI;
import dao.SaleReturnDaoI;
import pageModel.SaleReturn;
import service.SaleReturnServiceI;
@Service("saleReturnService")
public class SaleReturnServiceImpl implements SaleReturnServiceI {
private SaleReturnDaoI saleReturnDao;
private ReceiptDaoI receiptDao;

	public SaleReturnDaoI getSaleReturnDao() {
	return saleReturnDao;
}
@Autowired
public void setSaleReturnDao(SaleReturnDaoI saleReturnDao) {
	this.saleReturnDao = saleReturnDao;
}

public ReceiptDaoI getReceiptDao() {
	return receiptDao;
}
@Autowired
public void setReceiptDao(ReceiptDaoI receiptDao) {
	this.receiptDao = receiptDao;
}

	@Override
	public void save(SaleReturn saleReturn) {
		Tsalereturn t =new Tsalereturn();
		BeanUtils.copyProperties(saleReturn, t);
		t.setId(UUID.randomUUID().toString());
        t.setCreatedatetime(new Timestamp(System.currentTimeMillis()));
        if(saleReturn.getTid()!=null&&!saleReturn.getTid().trim().equals("")){
        	String hql = "from Treceipt t where t.tid=:tid";
			Map<String,Object>params=new HashMap<String,Object>();
			params.put("tid", saleReturn.getTid());
			Treceipt tr = receiptDao.get(hql, params);
			if(tr!=null){
				t.setTreceipt(tr);
			}
        }
        saleReturnDao.save(t);
        BeanUtils.copyProperties(t, saleReturn);
	}

}
